package emulator.util;


public class MemoryPtr {
	
	private byte [] memory;
	private int offset;

	public MemoryPtr(byte[] memory, int offset)
	{
		this.memory = memory;
		this.offset = offset;
	}
	
	public void get(byte[] raw_bytes) {
		for (int i = 0; i < raw_bytes.length; i++)
			raw_bytes[i] = memory[(i+offset) % memory.length];
	}

	public int getWordOp() {
		return (memory[(offset+1) % memory.length] & 0xFF) + ((memory[(offset+2) % memory.length] & 0xFF) << 8);
	}

	public int getWordOp(int disp) {
		return (memory[(offset+1+disp) % memory.length] & 0xFF) + ((memory[(offset+2+disp) % memory.length] & 0xFF) << 8);
	}
	
	public int getByteOp() {
		return memory[(offset+1) % memory.length] & 0xFF;
	}

	public long getByteOp(int disp) {
		return memory[(offset+1+disp) % memory.length] & 0xFF;
	}
	
	public int getRelativeOp() {
		int jumprel = getByteOp();
		if (jumprel > 127)
			jumprel -= 256;
		return offset+2+jumprel;
	}

	public long getAddress() {
		return offset+1;
	}

	public void setAddress(int address) {
		offset = address-1;
	}

}
